#include "huan1form.h"
#include "ui_huan1form.h"
#include "framecore.h"

Huan1Form::Huan1Form ( QWidget* parent ) :
    HuanFrame ( parent ),
    ui ( new Ui::Huan1Form )
{
    ui->setupUi ( this );
}

Huan1Form::~Huan1Form()
{
    delete ui;
}

/*
void Huan1Form::initButtons()
{
    //return;

    int index = 0;

    QQtDictionaryMutableListIterator itor ( gUsingLightsDict.getList() );
    while ( itor.hasNext() )
    {
        QQtDictionary& node = itor.next();

        if ( !node["type"].getValue().isValid() )
            continue;

        //孤零零 孤滴着
        if ( node["type"] == "light" )
        {
            Caidan1Button* btn = new Caidan1Button ( this );
            btn->lightNo() = node["lightNo."].getValue().toInt();
            btn->sceneName() = node["scene"].getValue().toString();
            QString icon = findIconNameBySceneName ( btn->sceneName() );
            btn->sceneIcon() = "./skin/demo/" + icon;
            //pline() << btn->lightNo() << btn->sceneName() << btn->sceneIcon();
            btn->updateWidget();
            node["button"].setType ( QQtDictionary::DictValue );
            node["button"].getValue().setValue<Caidan1Button*> ( btn ); //保存指针还是引用呢？
        }
        else if ( node["type"] == "group" )
        {
            Caidan1GroupButton* btnGroup = new Caidan1GroupButton ( this );

            QQtDictionaryMutableListIterator itor ( node.getList() );
            while ( itor.hasNext() )
            {
                QQtDictionary& node = itor.next();
                Caidan1Button* btn = new Caidan1Button ( btnGroup );
                btn->lightNo() = node["lightNo."].getValue().toInt();
                btn->sceneName() = node["scene"].getValue().toString();
                QString icon = findIconNameBySceneName ( btn->sceneName() );
                btn->sceneIcon() = "./skin/demo/" + icon;
                //pline() << btn->lightNo() << btn->sceneName() << btn->sceneIcon();
                btn->updateWidget();
                node["button"].setType ( QQtDictionary::DictValue );
                node["button"].getValue().setValue<Caidan1Button*> ( btn );//保存指针还是引用呢？
                //这里发生了诡异的事情，QList析构了一次元素，菜单group实例也析构了一次，导致重析构错误。
                //纠正：菜单btn和group btn都具备良好的临时变量和存储变量的能力。
                //QList当中，不应当存储于外界有干系的变量。
                btnGroup->AddCaidanButton ( *btn );
            }

            btnGroup->setText ( node["name"].getValue().toString() );
            //pline() << btnGroup->text() << node["lightNo."].getValue().toString();
            btnGroup->updateCaidanButton();
            node["button"].setType ( QQtDictionary::DictValue );
            node["button"].getValue().setValue<Caidan1GroupButton*> ( btnGroup );
        }

//        pline() << size;
//        pline() << node["button"];
//        pline() << node["type"];
//        pline() << node["name"];

        //排布 此处要求必须用指针。
        QWidget* widget = node["button"].getValue().value<QWidget*>();
        widget->setGeometry ( calculatePosition ( this, index,
                                                  this->width() / 2 - 40 - 10,
                                                  40,
                                                  10 ) );

        index++;
    }

    return;
}

void Huan1Form::initButtons2()
{

    int size = gUsingLightsDict.getList().size();

    for ( int i = 0; i < size; i++ )
    {
        QQtDictionary& node = gUsingLightsDict.getList() [i];

        if ( !node["type"].getValue().isValid() )
            continue;

        if ( node["type"] == "light" )
        {
            Caidan1Button* btn = new Caidan1Button ( this );
            Caidan1Button* srcBtn = node["button"].getValue().value<Caidan1Button*> ( );
            *btn = *srcBtn;
            btn->updateWidget();
        }
        else if ( node["type"] == "group" )
        {
            Caidan1GroupButton* btnGroup = new Caidan1GroupButton ( this );
            int size = node.getList().size();

            for ( int i = 0; i < size; i++ )
            {
                QQtDictionary& node = node.getList() [i];
                if ( !node["type"].getValue().isValid() )
                    continue;

                Caidan1Button* btn = new Caidan1Button ( btnGroup->widget() );
                //node当中必须已经存在button节点。
                Caidan1Button* srcBtn = node["button"].getValue().value<Caidan1Button*> ( );
                *btn = *srcBtn;
                btn->updateWidget();
                btnGroup->AddCaidanButton ( *btn );
            }

            btnGroup->setText ( node["name"].getValue().toString() );
            pline() << btnGroup->text() << node["lightNo."].getValue().toString();
            btnGroup->updateCaidanButton();
        }

        QWidget* widget = node["button"].getValue().value<QWidget*>();
        widget->setGeometry ( calculatePosition ( this, i,
                                                  this->width() / 2 - 40 - 10,
                                                  40,
                                                  10 ) );
    }

}
*/

void Huan1Form::initButtons3()
{
    int size = gUsingLightsDict.getList().size();

    for ( int i = 0; i < size; i++ )
    {
        QQtDictionary& node = gUsingLightsDict.getList() [i];

        if ( !node["type"].getValue().isValid() )
            continue;

        QQtClickWidget* posWidget = 0;

        if ( node["type"] == "light" )
        {
            Caidan1Button* btn = new Caidan1Button ( this );
            btn->lightNo() = node["lightNo."].getValue().toInt();
            btn->sceneName() = node["scene"].getValue().toString();
            QString icon = findIconNameBySceneName ( btn->sceneName() );
            btn->sceneIcon() = "./skin/demo/" + icon;
            //pline() << btn->lightNo() << btn->sceneName() << btn->sceneIcon();
            btn->updateWidget();
            posWidget = btn;
        }
        else if ( node["type"] == "group" )
        {
            Caidan1GroupButton* btnGroup = new Caidan1GroupButton ( this );
            int size = node.getList().size();
            QQtDictionary& tmp = node;
            for ( int i = 0; i < size; i++ )
            {
                QQtDictionary& node = tmp.getList() [i];

                if ( !node["type"].getValue().isValid() )
                    continue;

                Caidan1Button* btn = new Caidan1Button ( btnGroup->widget() );
                btn->lightNo() = node["lightNo."].getValue().toInt();
                btn->sceneName() = node["scene"].getValue().toString();
                QString icon = findIconNameBySceneName ( btn->sceneName() );
                btn->sceneIcon() = "./skin/demo/" + icon;
                //pline() << btn->lightNo() << btn->sceneName() << btn->sceneIcon();
                btn->updateWidget();
                /**
                 * QObject会帮助用户释放QWidget子孙的空间
                 * 如果Widget子孙保存在QList里，QList也会释放一次。new空间，delete空间。不是指针哦。
                 * 所以，引发二次free，导致崩溃。
                 * 解决方法：QList，内部保存指针。
                 */
                btnGroup->AddCaidanButton ( btn );
#if 0
                btn->setGeometry ( calculatePosition ( btnGroup->widget(), i,
                                                       btnGroup->widget()->width() / 2 - 40 - 10,
                                                       40,
                                                       size ) );
#endif

            }

            btnGroup->setText ( node["name"].getValue().toString() );
            pline() << btnGroup->text() << node["lightNo."].getValue().toString();
            btnGroup->updateCaidanButton();
            posWidget = btnGroup;
        }

        posWidget->setGeometry ( calculatePosition ( this, i,
                                                     this->width() / 2 - 40 - 10,
                                                     40,
                                                     10 ) );
    }
}
